<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>销课管理</title>
<meta name="renderer" content="webkit">	
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">	
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">	
<meta name="apple-mobile-web-app-status-bar-style" content="black">	
<meta name="apple-mobile-web-app-capable" content="yes">	
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/table.css">
<link rel="stylesheet" href="css/calendar.css">
</head>
<body>
<div class="content-head">悦宝园会员管理系统</div>
<div class="content-main">
	<div class="side-nav" ms-controller="navBar">
		<ul class="nav-item">
			<li><a href="main.html" class="nav-item-front nav-item-add">首页</a></li>
			<li ms-for="el in @navTitleArr">
				<a href="javascript:;" class="nav-item-front">{{el.par}}</a>
				<dl class="nav-item-child">
					<dd ms-for="($index, ex) in el.child"><a ms-attr='{href: el.href[$index]}'>{{ex}}</a></dd>
				</dl>
			</li>
		</ul>
	</div>
	<div class="content bg-gray" ms-controller="main">
		<div class="tab-content">
			<div class="tab-item">
				<div class="sub-main">
					<p class="sub-title"><i>销课管理</i></p>
					<div>
						<div class="week_content">
							<div class="week_title" id="week_title" ids="0">
								<a href="javascript:void(0)" id="prevWeek"></a>
								<h1 id="showDate"></h1>
								<a href="javascript:void(0)" id="nextWeek" class="nextWeek"></a>
							</div>
							<div class="week_box">
								<ul class="clear" id="weekUl"></ul>
							</div>
							<div class="calendar_box clear" id="calendarBox"></div>
						</div>
					</div>
					<div class="schedule-body">
						<div class="schedule-btns">
							<a href="javascript:;" id="subForm">确认销课</a>
						</div>
						<table>
							<tbody id="scheduleBody">
								<tr>
									<td valign="top" width="59">时间</td>
									<td valign="top" width="92">老师</td>
									<td valign="top" width="199">姓名</td>
									<td valign="top" width="323">电话</td>
									<td valign="top" width="323">状态</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
	</div> 
</div>
<div class='mask hide' id="mask"></div>
<div class="mask-content passClassContent hide" id="confirmContent" style="width: 450px; height: 130px;">
	<p>确认销课吗（该操作不可更改）？</p>
	<div>
		<a href="javascript:;" id="ok">确认</a>
		<a href="javascript:;" id="cancel">取消</a>
	</div>
</div>

<script src="js/jq.js"></script>
<script src="js/avalon.js"></script>
<script src="js/main.js"></script>
<script src="js/calendar.js"></script>
<script>
(function() {
	var cDate;
	var sData;
	var sObj = {};
	var scheduleData = baseFunc.getStorageData("schedule");
	var clickObj;
	var clickTime;
	var students = [];
	var teachers = [];
	var tempStudent = {};
	var classNum = 1;

	//初始化日历
	var sDate = baseFunc.getStorageData("schedule");
	var testData = [];
	if (sDate) {
		for (var i = 0; i < sDate.length; i++) {
			if(sDate[i].passStatus) {
				testData.push({date: sDate[i].date, items: [{className:'已核销'}]});
			} else {
				testData.push({date: sDate[i].date, items: [{className:'未核销'}]});
			}
			
		};
	};

	var diffDate = function(newDate, oldDate) { //{[], []}
		var year = newDate[0]; //表示需要查找的年份
		var month = newDate[1];//表示需要查找的月份
		var curMonthDays = new Date(year,month,0).getDate();//0表示3月的第0天，上月的最后一天,月份从0开始记数
		if(oldDate[2] > curMonthDays) {
			newDate[2] = curMonthDays;
		}

		return newDate[0] + "-" + newDate[1] + "-" + newDate[2];
	};

	var calcDate = function(arr, calcNum) {
		var oldY = parseInt(arr[0]),
			oldM = parseInt(arr[1]),
			oldD = parseInt(arr[2]),
			y = oldY,
			m = oldM,
			d = oldD,
			calcNum = parseInt(calcNum);

		if(calcNum == 12) {
			y += 1;
			return diffDate([y, m, d], [oldY, oldM, oldD]);
		}

		if(calcNum == 24) {
			y += 2;
			return diffDate([y, m, d], [oldY, oldM, oldD]);
		}

		if(calcNum < 12) {
			m = m + calcNum;
			if(m > 12) {
				y += 1;
				m -= 12;
			}
			return diffDate([y, m, d], [oldY, oldM, oldD]);
		}

		if(calcNum > 12) {
			y += 1;
			calcNum -= 12;
			m += calcNum;
			if(m > 12) {
				y += 1;
				m -= 12;
			}
			return diffDate([y, m, d], [oldY, oldM, oldD]);
		}
	};

	var inWhichTime = function(tipsDate, confirmDate) {
		//情况一刚好处于提醒日
		var day2 = new Date();
		day2.setTime(day2.getTime());
		var nowDate = day2.getFullYear() + "-" + (day2.getMonth()+1) + "-" + day2.getDate();
		return effDiff = baseFunc.dateDiff(tipsDate, nowDate);
	};

	weeklyCalendar(testData,{
		// 点击日期回调
		clickDate: function (date) {
			$(".alert-area").addClass("alert-visible");
			cDate = date;
			var data,
				htmls = "",
				classNum = 0,
				cc = false;

			$(".delete").remove();
			if(!scheduleData) {
				return;
			}
			sData = "";
			for (var i = 0; i < scheduleData.length; i++) {
				if(scheduleData[i].date == cDate) {
					data = sData = scheduleData[i];
					cc = true;
					break;
				}
			};

			if(!cc) {
				return;
			}

			var sTeacher = "",
				sName,
				sTel,
				sStatus,
				iSame = true;
			for (var k = 0; k < data["info"].length; k++) {
				if(data["info"][k].classType == "gym") {
					classNum = 11;
				} else {
					classNum = 9;
				}
				sTeacher = "";
				if(data["info"][k]["teachers"]) {
					$.each(data["info"][k]["teachers"], function(i, elem) {
						if(i == 0) {
							sTeacher = elem["name"];
						} else if (i == 1) {
							sTeacher = sTeacher + "," + elem["name"];
						}
						
					})
				}
				//初始化表格
				htmls = '<tr class="delete"><td rowspan="'+classNum+'" valign="top" width="59"><p>'+data["info"][k]["time"]+'</p><p>'+data["info"][k]["classFullName"]+'</p></td><td data-type="teacher" data-info="'+cDate+','+data["info"][k]["time"]+','+data["info"][k]["className"]+','+data["info"][k]["classFullName"]+'" rowspan="'+classNum+'" valign="top" width="92">'+sTeacher+'</td>';

				for (var q = 0; q < classNum; q++) {
					sName = sTel = sStatus = "";
					if(data["info"][k]["students"] && q < data["info"][k]["students"].length) {
						sName = data["info"][k]["students"][q]["name"];
						if(data["info"][k]["students"][q]["tel"]) {
							sTel = data["info"][k]["students"][q]["tel"] ? data["info"][k]["students"][q]["tel"] : "";
						}
						if(data["info"][k]["students"][q]["cc"]) {
							sStatus = data["info"][k]["students"][q]["cc"] ? data["info"][k]["students"][q]["cc"] : "";
						} else {
							var firstArr = baseFunc.getStorageData("member", {flag: data["info"][k]["students"][q]["flag"]})[0]["firstClass"];
							if(!firstArr) {
								sStatus = "首次上本课程";
							} else {
								for (var x = 0; x < firstArr.length; x++) {
									if(firstArr[x].className == data["info"][k]["className"]) {
										iSame = false;
									}
								};
								sStatus = iSame ? "首次上本课程" : "";
							}
						}
					}
					
					if(q == 0) {
						htmls += '<td class="info" data-type="member" valign="top" width="199">'+sName+'</td><td valign="top" width="323">'+sTel+'</td><td valign="top" width="323">'+sStatus+'</td></tr>';
					} else {
						htmls += '<tr class="delete"><td class="info" valign="top" data-type="member">'+sName+'</td><td valign="top" width="323">'+sTel+'</td><td valign="top" width="323">'+sStatus+'</td></tr>';
					}
				};

				$("#scheduleBody").append(htmls);
			};
		},
		// 点击弹窗回调
		clickDownLoad: function (that) {
			console.log(that)
		}
	}, true);

	$(".current").trigger("click");

	//关闭
	$(".mask").click(function() {
		$("#mask").addClass("hide");
		$("#confirmContent").addClass("hide");
	});
	$("#cancel").click(function() {
		$("#mask").addClass("hide");
		$("#confirmContent").addClass("hide");
	});

	//提交销课确认
	$("#subForm").click(function() {
		var passInfo = {};

		if(!sData) {
			baseFunc.alert("请选择需要核销的课表");
			return;
		}

		if(sData.passStatus) {
			baseFunc.alert("该课表已核销");
			return;
		}

		$("#mask").removeClass("hide");
		$("#confirmContent").removeClass("hide");
	});

	$("body").on("click", "#ok", function() {
		var passS;

		for (var i = 0; i < scheduleData.length; i++) {
			if(scheduleData[i].date == cDate) {
				passS = scheduleData[i];
				break;
			}
		};
		
		for(var k = 0; k < passS.info.length; k++) {
			var thisInfo = passS.info[k];
			//先判断是否选择老师
			if(thisInfo.students.length > 0 && (!thisInfo.teachers || thisInfo.teachers.length == 0)) {
				$("#mask").addClass("hide");
				$("#confirmContent").addClass("hide");
				baseFunc.alert("请选择 【"+thisInfo["classFullName"]+"】 上课老师");
				return;
			}
			//
			var thisStudents = thisInfo["students"];
			for (var n = 0; n < thisStudents.length; n++) {
				if (thisStudents[n].type == "member") {
					var thisStudentFlag = thisStudents[n].flag;
					var thisStudentInfo = baseFunc.getStorageData("member", {flag: thisStudentFlag})[0];
					var isSameClass = true;
					var classNum = passS.info[k].className == "ra" ? 2 : 1;
					var birthday = thisStudentInfo.birthday.split("-");

					/************** 十月以内不销课时 *************/
					var isOld = inWhichTime(calcDate(birthday, 10));

					//本用户是否有足够课时销课
					var thisStudentTotalClassNum = parseInt(thisStudentInfo.classNum.formal) + parseInt(thisStudentInfo.classNum.free);
					if(thisStudentTotalClassNum - classNum < 0) {
						$("#mask").addClass("hide");
						$("#confirmContent").addClass("hide");
						baseFunc.alert(thisStudentInfo.name + "课时包已用完");
					}

					if(thisStudentInfo.classNum.formal >= classNum) {
						if(isOld > 0) {
							thisStudentInfo.classNum.formal -= classNum; 
						}
					} else {
						thisStudentInfo.classNum.formal = 0;
						thisStudentInfo.classNum.free -= thisStudentInfo.classNum.formal;
					}

					//关联账户同时销课
					if(thisStudentInfo.connect) { 
						var pMember = baseFunc.getStorageData("member", {flag: thisStudentInfo.connect.flag})[0];
						var thisStudentTotalClassNum = parseInt(pMember.classNum.formal) + parseInt(pMember.classNum.free);
						if(thisStudentTotalClassNum - classNum < 0) {
							$("#mask").addClass("hide");
							$("#confirmContent").addClass("hide");
							baseFunc.alert(pMember.name + "课时包已用完");
						}

						if(pMember.classNum.formal >= classNum) {
							if(isOld > 0) {
								pMember.classNum.formal -= classNum;
							} 
						} else {
							pMember.classNum.formal = 0;
							pMember.classNum.free -= pMember.classNum.formal;
						}

						baseFunc.editStorageData("member", pMember);
					}

					//销课
					if(!thisStudentInfo.passClass) { 
						thisStudentInfo.passClass = [];
					}

					thisStudentInfo.passClass.push({
						date: cDate,
						time: passS.info[k].time,
						className: passS.info[k].className,
						classFullName: passS.info[k].classFullName,
						num: classNum,
						type: isOld > 0 ? "normal" : "free"
					});

					//首次上课
					if(!thisStudentInfo.firstClass) { 
						thisStudentInfo.firstClass = []
						thisStudentInfo.firstClass.push({
							className: passS.info[k].className,
							classFullName: passS.info[k].classFullName,
							data: cDate
						});
					} else {
						for (var y = 0; y < thisStudentInfo.firstClass.length; y++) {
							if(thisStudentInfo.firstClass[y].className == passS.info[k].className) {
								isSameClass = false;
								break;
							}
						};
						if (isSameClass) {
							thisStudentInfo.firstClass.push({
								className: passS.info[k].className,
								classFullName: passS.info[k].classFullName,
								data: cDate
							});
						};
					}
					baseFunc.editStorageData("member", thisStudentInfo);
				};
				passS.passStatus = true;
				baseFunc.editStorageData("schedule", passS);
				setTimeout(function() {
					$("#mask").addClass("hide");
					$("#confirmContent").addClass("hide");
					baseFunc.alert("销课成功", "green");
				}, 300);
			};
		}
	});
})();
</script>
</body>
</html>